<?php
ob_start();
include("common/header.html");
include("common/left.html");
$out_html=ob_get_contents();
ob_end_clean();

include("common/page.php");
$_a->authVerify();

$dtypeT=array(0=>"购买产品",1=>"撤改订单",3=>"账户资金变化",4=>"供应商授信余额",5=>"利润",6=>"提现冻结",7=>"电子凭证费",8=>"短信息费",9=>"银行交易手续费",10=>"凭证费",11=>"供应商信用额度变化 ",12=>"取消提现",13=>'拒绝提现');
$ptypeT=array(-1=>"支付宝",0=>"账号资金",1=>"支付宝",2=>"供应商可用资金",3=>"供应授信额度设置");
$r=array();
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{
	$offset=0;$pageSize=999999999;
	// $offset=0;$pageSize=9;
}


$where1=$where2="where 1=1";
$where1.=" and dmoney>0 ";
$dname=$_REQUEST['dname'];
if($dname&&is_numeric($dname)&&strlen($dname)==6){
    $str="select id from pft_member where account='$dname'";
    $GLOBALS['le']->query($str);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $ids[]=$row['id'];
    }
    if(count($ids)!=0){
		$ids=implode(",",$ids);
		$where1.=" and fid in ($ids)";
		$where2.=" and fid in ($ids)";
	}else{
		$where1.=" and 1<>1";
		$where2.=" and 1<>1";
	}
}elseif($dname=mysql_real_escape_string(trim($_REQUEST['dname']))){
	$ids=array();
	$str="select id from pft_member where dname like '%".$dname."%'";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$ids[]=$row['id'];
	}
	if(count($ids)!=0){
		$ids=implode(",",$ids);
		$where1.=" and fid in ($ids)";
		$where2.=" and fid in ($ids)";
	}else{
		$where1.=" and 1<>1";
		$where2.=" and 1<>1";
	}
}
if($beginDate=mysql_real_escape_string($_REQUEST['beginDate'])){
	$where1.=" and rectime>='$beginDate 00:00:00'";
	$where2.=" and rectime>='$beginDate 00:00:00'";
}
if($endDate=mysql_real_escape_string($_REQUEST['endDate'])){
	$where1.=" and rectime<='$endDate 23:59:59'";
	$where2.=" and rectime<='$endDate 23:59:59'";
}
if($_REQUEST['daction']==="0"||$_REQUEST['daction']==="1"){
	$daction=$_REQUEST['daction'];
	$where1.=" and daction=$daction";
}
if($_REQUEST['dtype']||$_REQUEST['dtype']==="0"){
	$where1.=" and dtype=".$_REQUEST['dtype'];
	$where2="where -1=1";
}
if($_REQUEST['account']=="pft"){
	$where1.=" and ptype in (0,1)";
	$where2="where -1=1";
}
if($_REQUEST['account']=="credit"){
	$where1.=" and ptype=2";
	$where2="where -1=1";
}
if($_REQUEST['account']=="creditSet"){
	$where1.=" and ptype=3";
	$where2="where -1=1";
}
if($_REQUEST['account']=="alipay"){
	$where1="where -1=1";
}
if($_REQUEST['orderid']){
    $where1 .= " and orderid={$_REQUEST['orderid']}";
    $where2 .= " and orderid={$_REQUEST['orderid']}";
}
$dnames=array();
$str="select id,dname,account from pft_member";
$GLOBALS['le']->query($str);
$dn=array();
while($row=$GLOBALS['le']->fetch_assoc()){
    $dn[$row['id'].'_'.$row['account']]=$row['dname'];
	$dnames[$row['id']]=$row['dname'];
}
$ainfo=$t=array();
foreach($dn as $key=>$d){
    $k=explode('_',$key);
    $t['id']=$k[0];
    $t['dname']=$d;
    $t['account']=$k[1];
    $ainfo[]=$t;
    unset($t);
}
// if($_REQUEST['account']=='') $_REQUEST['account']=='alipay';
if($_REQUEST['account']=='alipay' || $_REQUEST['account']==''){
	// $str="select id,fid,money as dmoney,'' as lmoney,'' as aid,orderid,0 as daction,rectime,-1 as dtype,-1 as ptype,ali_account from pft_member_alipay $where2 order by rectime desc,orderid desc limit 0,10";
    $str="select id,fid,money as dmoney,'' as lmoney,'' as aid,orderid,daction,rectime,dtype,-1 as ptype,ali_account from pft_member_alipay $where2 order by rectime desc,orderid desc limit 0,10";
}else{
	$str="select id,fid,dmoney,lmoney,aid,orderid,daction,rectime,dtype,ptype,'' as ali_account from pft_member_journal $where1 order by rectime desc,orderid desc,id desc limit $offset,$pageSize";
}
// $str="(select id,fid,dmoney,lmoney,aid,orderid,daction,rectime,dtype,ptype,'' as ali_account from pft_member_journal $where1) UNION (select id,fid,money,'','',orderid,0,rectime,-1,-1,ali_account from pft_member_alipay  $where2) order by rectime desc,orderid desc limit $offset,$pageSize";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
	if($row['daction']==0){
		$row['income']="+".$row['dmoney']/100;
	}else{
		$row['pay']="-".$row['dmoney']/100;
	}
	if($row['dtype']==-1){//鼓浪屿专用 不过ali_account都是空
		$row['dtypeT']="支付宝账号".$row['ali_account'];
	}
	$r[]=$row;
}
//当查询的是提现冻结时 判断下是否已取消
foreach($r as $k => $v){
    if($v['pay']!='' && $v['dtype'] == '6'){
        $sel_status = "select wd_status from pft_wd_cash where fid={$v['fid']} and apply_time='{$v['rectime']}' limit 1";
        $GLOBALS['le']->query($sel_status);
        $GLOBALS['le']->fetch_assoc();
        $wd_status = $le->f('wd_status');
        if($wd_status == '3'){
            for($i=1;$i<20;++$i){
                if($r[$k-$i]['fid'] == $v['fid']){
                    $r[$k-$i]['dtype'] = 12;
                    continue 2;
                }
            }
        }
        if($wd_status == '2'){ 
            for($i=1;$i<20;++$i){
                if($r[$k-$i]['fid'] == $v['fid']){
                    $r[$k-$i]['dtype'] = 13;
                    continue 2;
                }
            }
        }
    }
}


if($_REQUEST['cs']){
    echo $str;print_r($r);exit;
}
// exit;
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{   
	$filename=date('YmdHis').'交易记录';
	$Excel[0]['member']='交易商户';
	$Excel[0]['rectime']='交易时间';
	$Excel[0]['orderid']='订单号';
	$Excel[0]['dtype']='交易内容';
	$Excel[0]['ptype']='交易方式';
	$Excel[0]['pay']='收入';
    if($row['ptype']==0){
        $Excel[0]['pay_out']='支出';//2015-1-9新加 By zax
    }
	$Excel[0]['alipay']='银行账户';
	$Excel[0]['lmoney']='账户余额';
	$Excel[0]['lmoney2']='授信余额';
	$i=1;
	foreach($r as $lid=>$row){
		$n++;
        // if(strpos($dnames[$row['fid']],'测试')!==false)continue;
		$Excel[$n]['member']=$dnames[$row['fid']];
		$Excel[$n]['rectime']=$row['rectime'];
		$Excel[$n]['orderid']=$row['orderid'] ? $row['orderid']:$row['out_trade_no'];
		$Excel[$n]['dtype']=$dtypeT[$row['dtype']] ? $dtypeT[$row['dtype']]:$row['dtypeT'];
		$Excel[$n]['ptype']=$ptypeT[$row['ptype']];
        // if($row['ptype']==0){
            $Excel[$n]['pay']=$row['income'];
        // }else{
            // $Excel[$n]['pay']=strip_tags($row['pay']);
        // }
         // if($row['ptype']==0){
            $Excel[$n]['pay_out'] = strip_tags($row['pay']);
		// }
        $Excel[$n]['alipay']=$row['alipay'];
        $Excel[$n]['lmoney']=($row['ptype']==0||$row['ptype']==1)?$row['lmoney']/100:"";
		$Excel[$n]['lmoney2']=($row['ptype']==2||$row['ptype']==3)?($row['lmoney']/100)."(".$dnames[$row['aid']].")":"";
	}
	$xls = new SimpleExcel('UTF-8', true,'orderList');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
	exit;
}


if($_REQUEST['account']=='alipay' || $_REQUEST['account']==''){
    $str="select count(*) as total from pft_member_alipay $where2";
    $GLOBALS['le']->query($str);
    $GLOBALS['le']->fetch_assoc();
    $count=$GLOBALS['le']->f("total");
}else{
    $str="select count(*) as total from pft_member_journal $where1";
    $GLOBALS['le']->query($str);
    $GLOBALS['le']->fetch_assoc();
    $count=$GLOBALS['le']->f("total");
}
// $count=$count1+$count2;
// var_dump($count);
$currentPage=$_REQUEST['currentPage']?abs($_REQUEST['currentPage']):1;
$pageSize=$_REQUEST['pageSize']?abs($_REQUEST['pageSize']):10;
$offset=($currentPage-1)*$pageSize;
echo $out_html;
include("tpl/accountTrecord.tpl");
include("common/footer.html");
?>

